package ${group}.${artifact}.core.controller.${moduleName};

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${group}.common.core.annotation.AuthUser;
import ${group}.common.core.pojo.Result;
import ${group}.common.core.pojo.UserCache;
import ${group}.common.mybatisplus.pojo.PageList;
import ${group}.common.mybatisplus.pojo.PageModel;
import ${group}.common.mybatisplus.pojo.Query;
import ${group}.${artifact}.core.controller.${moduleName}.dto.in.${className}SaveDto;
import ${group}.${artifact}.core.controller.${moduleName}.dto.in.${className}SelectDto;
import ${group}.${artifact}.core.mapper.${moduleName}.pojo.${className};
import ${group}.${artifact}.core.service.${moduleName}.${className}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * ${desc} Controller
 *
 * @author ${author}
 * @date ${nowDateTime}
 */
@RestController
@RequestMapping("/${classNameLower}")
@Api(value = "/${classNameLower}", tags = "${desc}")
public class ${className}Controller {

    @Autowired
    private ${className}Service ${classNameLower}Service;

    @PostMapping
    @ApiOperation(value = "${desc}新增", notes = "${desc}新增")
    Result insert(@Validated @RequestBody ${className}SaveDto ${classNameLower}SaveDto, @AuthUser UserCache userCache) {
        ${className} ${classNameLower} = BeanUtil.toBean(${classNameLower}SaveDto, ${className}.class);
        ${classNameLower}Service.save(${classNameLower});
        return Result.ok();
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "${desc}删除", notes = "${desc}删除")
    @ApiImplicitParams(@ApiImplicitParam(name = "ids", value = "主键id,多个逗号分隔"))
    Result delete(@PathVariable String ids, @AuthUser UserCache userCache) {
        if (StrUtil.isBlank(ids)) {
            return Result.error("ids不能为空");
        }
        ${classNameLower}Service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok();
    }


    @PutMapping("/{id}")
    @ApiOperation(value = "${desc}修改", notes = "${desc}修改")
    @ApiImplicitParams(@ApiImplicitParam(name = "id", value = "用户id"))
    Result update(@PathVariable Long id, @Validated @RequestBody ${className}SaveDto ${classNameLower}SaveDto, @AuthUser UserCache userCache) {
        ${className} ${classNameLower} = ${classNameLower}Service.getById(id);
        if (${classNameLower} == null) {
            return Result.error("${desc}无此数据");
        }
        BeanUtil.copyProperties(${classNameLower}SaveDto, ${classNameLower}, CopyOptions.create().ignoreNullValue());

        ${classNameLower}Service.updateById(${classNameLower});
        return Result.ok();
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "${desc}详情", notes = "${desc}详情")
    @ApiImplicitParams(@ApiImplicitParam(name = "id", value = "用户id"))
    Result<${className}> select(@PathVariable Long id) {
        ${className} ${classNameLower} = ${classNameLower}Service.getById(id);
        if (${classNameLower} == null) {
            return Result.error("${desc}无此数据");
        }
        return Result.ok(${classNameLower});
    }

    @GetMapping
    @ApiOperation(value = "${desc}分页查询", notes = "${desc}分页查询")
    Result<PageList<${className}>> select(${className}SelectDto ${classNameLower}SelectDto, PageModel pageModel) {
        //分页查询
        Query<${className}> query = new Query<>();
        //分页参数
        Page<${className}> queryPage = query.getPage(pageModel);
        //查询条件参数 默认构建所有的字段且条件为EQ  自定义请参照getWrapper()
        QueryWrapper<${className}> queryWrapper = query.getWrapper(${classNameLower}SelectDto);
        //执行查询
        IPage<${className}> page = ${classNameLower}Service.page(queryPage, queryWrapper);
        return Result.ok(new PageList<${className}>(page));
    }

}
